package com.xk857.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xk857.article.mapper.CategoryMapper;
import com.xk857.article.req.CategoryREQ;
import com.xk857.article.service.ICategoryService;
import com.xk857.entities.Category;
import com.xk857.util.base.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author CV大魔王
 * @Description 模块-业务层实现类
 * @date 2021/2/21 14:15
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Override
    public IPage<Category> queryPage(CategoryREQ req) {
        // 查询条件对象
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        // 判断模块名称是否为空，如果不为空，则拼接查询条件
        if (StringUtils.isNotEmpty(req.getName())) {
            wrapper.like("name", "%" + req.getName() + "%");
        }
        // 判断状态是否为空，如果不为空，则拼接查询条件
        if (req.getStatus() != null) {
            wrapper.eq("status", req.getStatus());
        }
        // 排序
        wrapper.orderByDesc("status").orderByAsc("sort");
        // 第1个参数是Page分页对象，第2个参数是查询条件
        return baseMapper.selectPage(req.getPage(), wrapper);
    }

    @Override
    public void removeByName(String name) {
        baseMapper.delete(new QueryWrapper<Category>().eq("name", name));
    }

    @Override
    public List<Category> findAllNormal() {
        return baseMapper.selectList(new QueryWrapper<Category>().eq("status", 1));
    }

    @Override
    public List<Category> findCategoryAndLabel() {
        return baseMapper.findCategoryAndLabel();
    }
}
